package com.chen.solution.validator.demo.controller;/**
 * @author Chentian
 * @date 2022/3/16
 */

import com.chen.solution.validator.demo.dto.AccountDto;
import com.chen.solution.validator.demo.dto.GenericAccountResponse;
import com.chen.solution.validator.demo.dto.GetOrCreateRequest;
import com.chen.solution.validator.demo.validation.PhoneNumber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.UUID;

/**
 *
 * @author Chentian
 * @date 2022/3/16
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/account")
public class AccountController {

    @PostMapping("/getOrCreate")
    public GenericAccountResponse getOrCreate(@RequestBody @Valid GetOrCreateRequest request) {
        log.info("name:{},phoneNumber:{}", request.getName(), request.getPhoneNumber());
        AccountDto accountDto = AccountDto.builder().id(UUID.randomUUID().toString())
                .name(request.getName())
                .email(request.getEmail())
                .phoneNumber(request.getPhoneNumber())
                .build();
        GenericAccountResponse genericAccountResponse = new GenericAccountResponse(accountDto);
        return genericAccountResponse;
    }

    @GetMapping(path = "/getByPhoneNumber")
    public GenericAccountResponse getByPhoneNumber(@RequestParam @PhoneNumber String phoneNumber) {
        log.info("phoneNumber:{}", phoneNumber);
        AccountDto accountDto = AccountDto.builder().phoneNumber(phoneNumber).name("chen").build();
        GenericAccountResponse genericAccountResponse = new GenericAccountResponse(accountDto);
        return genericAccountResponse;
    }

}
